<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\models\Area;
use app\models\Region;
use app\logic\PositionLogic;
use app\models\RegionBak;
use Yii;

use CFPropertyList\CFPropertyList;


class PositionController extends BaseController
{

	public function actionIndex()
	{
            echo "index";
	}
    /**
     * 获取精准的地址编码，供后面调用
     * @apiDescription   by Jim
     * @api              {post} /position/get-region-code 获取精准的地址编码，供后面调用
     * @apiName          get-region-code
     * @apiVersion       1.0.0
     * @apiParam {string} city 城市名
     * @apiParam {string} district 区名
     * * @apiParam {string} street 街道名
     * @apiGroup         Position
     * @apiSampleRequest /position/get-region-code?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
     *      region_code:110101001
     * }
     * @apiError         none
     */
	public function actionGetRegionCode(){
        $city = $this->getQueryparams('city','');
        $district = $this->getQueryparams('district','');
        $street = $this->getQueryparams('street','');
        $positionLogic = new PositionLogic();
        $result = $positionLogic->getRegionCode($city,$district,$street);
        $this->response(['region_code'=>$result]) ;
    }
    /**
     * 获取城市列表
     * @apiDescription   by Jim
     * @api              {post} /position/get-city-list 获取城市列表
     * @apiName          get-city-list
     * @apiVersion       1.0.0
     * @apiGroup         Position
     * @apiSampleRequest /position/get-city-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
           'A':{
    `region_code` varchar(45) NOT NULL,
    `parent_code` varchar(45) DEFAULT NULL,
    `region_name` varchar(45) DEFAULT NULL,
    `region_level` varchar(45) DEFAULT NULL,
    `firstchar` varchar(45) DEFAULT NULL,
    `remark` varchar(255) DEFAULT NULL,
     }
     * }]
     * @apiError         none
     */
	public function actionGetCityList(){
        $positionLogic = new PositionLogic();
        $cityList = $positionLogic->getCityList();
        $result = [];
        foreach($cityList AS $key=>$value){
            $result[$value['firstchar']][] = $value;
        }
        $this->response($result);
    }
    /**
     * 获取地区列表
     * @apiDescription   by Jim
     * @api              {post} /position/get-district-list 获取地区列表
     * @apiName          get-district-list
     * @apiVersion       1.0.0
     * @apiParam {string} region_code 城市列表中获取的region_code
     * @apiGroup         Position
     * @apiSampleRequest /position/get-district-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    `region_code` varchar(45) NOT NULL,
    `parent_code` varchar(45) DEFAULT NULL,
    `region_name` varchar(45) DEFAULT NULL,
    `region_level` varchar(45) DEFAULT NULL,
    `firstchar` varchar(45) DEFAULT NULL,
    `remark` varchar(255) DEFAULT NULL,

      }]
     * @apiError         none
     */
    public function actionGetDistrictList(){
        $region_code = $this->getQueryparams('region_code','');
        $positionLogic = new PositionLogic();
        $districtList = $positionLogic->getDistrictList($region_code);
        $this->response($districtList);
    }
    /**
     * 获取街道列表
     * @apiDescription   by Jim
     * @api              {post} /position/get-street-list 获取街道列表
     * @apiName          get-street-list
     * @apiVersion       1.0.0
     * @apiParam {string} region_code 地区列表中获取的region_code
     * @apiGroup         Position
     * @apiSampleRequest /position/get-street-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    `region_code` varchar(45) NOT NULL,
    `parent_code` varchar(45) DEFAULT NULL,
    `region_name` varchar(45) DEFAULT NULL,
    `region_level` varchar(45) DEFAULT NULL,
    `firstchar` varchar(45) DEFAULT NULL,
    `remark` varchar(255) DEFAULT NULL, 周边,
    `shop_count` int 商店数量

    }]
     * @apiError         none
     */
    public function actionGetStreetList(){
        $region_code = $this->getQueryparams('region_code','');
        $positionLogic = new PositionLogic();
        $streetList = $positionLogic->getStreetList($region_code);
        $this->response($streetList);
    }

    public function actionFixPosition(){
        //require_once(__DIR__.'/../classes/CFPropertyList/CFPropertyList.php');
        //var_dump( __DIR__.'/sample.xml.plist');exit;
        $plist = new CFPropertyList( __DIR__.'/../web/list.plist', CFPropertyList::FORMAT_XML );
        $plist = $plist->toArray();
        foreach($plist AS $key=>$value){
            if(!in_array($key,['110000北京市','120000天津市','310000上海市','440300深圳市','500000重庆市'])){
                foreach($value AS $ke=>$va){
                    $area = new Area();
                    $area->region_code = substr($ke,0,6);
                    $parent_code = substr($ke,0,6);
                    $area->parent_code = 0;
                    $area->region_name = substr($ke,6);
                    $area->region_level = 1;
                    $rb = RegionBak::findOne(['region_name'=>$area->region_name]);
                    $area->firstchar = $rb->firstchar;
                    $area->save();
                    foreach($va AS $k=>$v){
                        $area = new Area();
                        $area->region_code = substr($v,0,6);
                        $area->parent_code = $parent_code;
                        $parent_code1 = substr($v,0,6);
                        $area->region_name = substr($v,6);
                        $area->region_level = 2;
                        $area->save();
                        $dist_json = @file_get_contents('http://passer-by.com/data_location/town/'.$area->region_code.'.json');
                        if($dist_json){
                            $final_arr = json_decode($dist_json,true);
                            foreach ($final_arr AS $kk=>$vv){
                                $area = new Area();
                                $area->region_code = $kk;
                                $area->parent_code = $parent_code1;
                                $area->region_name = $vv;
                                $area->region_level = 3;
                                $area->save();
                            }
                        }

                    }
                }
            }else{
                $area = new Area();
                $area->region_code = substr($key,0,6);
                $parent_code = substr($key,0,6);
                $area->parent_code = 0;
                $area->region_name = substr($key,6);
                $area->region_level = 1;
                $rb = RegionBak::findOne(['region_name'=>$area->region_name]);
                $area->firstchar = $rb->firstchar;
                $area->save();
                foreach($value AS $ke=>$va){
                    $area = new Area();
                    $area->region_code = substr($va,0,6);
                    $area->parent_code = $parent_code;
                    $parent_code1 = substr($va,0,6);
                    $area->region_name = substr($va,6);
                    $area->region_level = 2;
                    $area->save();
                    $dist_json = file_get_contents('http://passer-by.com/data_location/town/'.$area->region_code.'.json');
                    if($dist_json){
                        $final_arr = json_decode($dist_json,true);
                        foreach ($final_arr AS $kk=>$vv){
                            $area = new Area();
                            $area->region_code = $kk;
                            $area->parent_code = $parent_code1;
                            $area->region_name = $vv;
                            $area->region_level = 3;
                            $area->save();
                        }
                    }

                }
            }
        }
    }

}